package com.ruoyi.settled.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.settled.domain.AgreementRule;
import com.ruoyi.settled.service.IAgreementRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 协议规则Controller
 * 
 * @author huangchen
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/rule/rule")
public class AgreementRuleController extends BaseController
{
    @Autowired
    private IAgreementRuleService agreementRuleService;

    /**
     * 查询协议规则列表
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgreementRule agreementRule)
    {
        startPage();
        List<AgreementRule> list = agreementRuleService.selectAgreementRuleList(agreementRule);
        return getDataTable(list);
    }

    /**
     * 导出协议规则列表
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:export')")
    @Log(title = "协议规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgreementRule agreementRule)
    {
        List<AgreementRule> list = agreementRuleService.selectAgreementRuleList(agreementRule);
        ExcelUtil<AgreementRule> util = new ExcelUtil<AgreementRule>(AgreementRule.class);
        util.exportExcel(response, list, "协议规则数据");
    }

    /**
     * 获取协议规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:query')")
    @GetMapping(value = "/{ruleCode}")
    public AjaxResult getInfo(@PathVariable("ruleCode") String ruleCode)
    {
        return success(agreementRuleService.selectAgreementRuleByRuleCode(ruleCode));
    }

    /**
     * 新增协议规则
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:add')")
    @Log(title = "协议规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgreementRule agreementRule)
    {
        return toAjax(agreementRuleService.insertAgreementRule(agreementRule));
    }

    /**
     * 修改协议规则
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:edit')")
    @Log(title = "协议规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgreementRule agreementRule)
    {
        return toAjax(agreementRuleService.updateAgreementRule(agreementRule));
    }

    /**
     * 删除协议规则
     */
    @PreAuthorize("@ss.hasPermi('rule:rule:remove')")
    @Log(title = "协议规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ruleCodes}")
    public AjaxResult remove(@PathVariable String[] ruleCodes)
    {
        return toAjax(agreementRuleService.deleteAgreementRuleByRuleCodes(ruleCodes));
    }
}
